package com.javathree;

import java.util.Scanner;

public class Demo5 {
    //点餐流程：收集点餐信息，累加
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        //订单
        Order[] orders = new Order[5];
        //用户列表
        User[] users = new User[5];
        //餐袋
        Menu[]  menusorder = new Menu[5];
        //菜单的容器
        Menu[] menus = new Menu[4];
        Menu menu = new Menu();
        menu.id=1;
        menu.mName="红烧肉";
        menu.mPrice=28.5;

        Menu menu1 = new Menu();
        menu1.id=2;
        menu1.mName="炒青菜";
        menu1.mPrice=8;

        Menu menu2 = new Menu();
        menu2.id=3;
        menu2.mName="西红柿炒鸡蛋";
        menu2.mPrice=9;


        Menu menu3 = new Menu();
        menu3.id=4;
        menu3.mName="佛跳墙";
        menu3.mPrice=999;

        menus[0]=menu;
        menus[1]=menu1;
        menus[2]=menu2;
        menus[3]=menu3;


        System.out.println("------欢迎进入KH62点餐系统--------");
        boolean isOk=true;
        //用户模块登录注册
        do {
            System.out.println("1:登录，2：注册");
            System.out.println("请选择您操作：");
            int i1 = scanner.nextInt();
            if(i1==1){
                //存，取--->登录流程:根据用户名和密码比对
                System.out.println("请输入用户名：");
                String uname = scanner.next();
                System.out.println("请输入密码：");
                String password = scanner.next();

                boolean isLogin = login(users, uname, password);
                if(isLogin){
                    System.out.println("登录成功...");
                    isOk=false;
                }else {
                    System.out.println("登录失败，请重新操作！");
                }

            }else if(i1==2){
                boolean isflag=true;
                do {
                    System.out.println("请输入用户名：");
                    String uname = scanner.next();
                    System.out.println("请输入密码：");
                    String password = scanner.next();

                    User user = new User();
                    user.name=uname;
                    user.password=password;
                    user.id=001;
                    //验证用户名是否存在
                    boolean  b= checkName(users,uname);
                    if(b){
                        for (int i=0;i<users.length;i++){
                            if(users[i]==null) {
                                users[i]=user;
                                System.out.println("是否继续注册(y/n)");
                                String next = scanner.next();
                                if(!next.equals("y")){
                                    isflag=false;
                                }
                                break;
                            }
                        }
                    }else {
                        System.out.println("用户名存在,请重新录入");
                    }
                }while (isflag);
                System.out.println("注册成功....");

            }
        }while (isOk);

        //点餐流程
        boolean isGo=true;
        do {
            System.out.println("1：查看菜单");
            System.out.println("2：查看餐袋");
            System.out.println("3：付款");
            System.out.println("4：查看订单");
            System.out.println("5：会员卡管理模块");
            System.out.println("6：退款");
            System.out.println("7：评论");
            System.out.println("请选择您需要的操作：");
            int i1 = scanner.nextInt();
            switch (i1){
                case 1:
                    System.out.println("编号\t\t\t\t名称\t\t\t价格\t\t\t数量");
                    for (int i=0;i<menus.length;i++){
                        System.out.println(menus[i].id+"\t\t\t\t"+menus[i].mName+"\t\t\t"+menus[i].mPrice+"\t\t\t"+menus[i].count);
                    }
                    boolean isflag=true;
                    do {
                        //开始点餐
                        System.out.println("请输入菜品编号");
                        int ids = scanner.nextInt();
                        //验证是否是 老菜
                        boolean b = checkMenuName(menusorder, ids);
                        //查找菜品
                        Menu menu4 = checkFromMenuList(menus, ids);
                        //处理新菜
                        if(b && menu4 !=null){
                            //录入菜品
                            for (int i=0;i<menusorder.length;i++){
                                if (menusorder[i] == null) {
                                    menu4.count++;
                                    menusorder[i]=menu4;
                                    break;
                                }
                            }
                        }
                        System.out.println("是否需要继续点餐（y/n）");
                        String next = scanner.next();
                        if (next.equals("n")){
                            isflag=false;
                        }
                    }while (isflag);
                    break;

                case 2:
                    //todo 查看餐袋
                    System.out.println("您的餐袋如下：");
                    System.out.println("编号\t\t\t\t名称\t\t\t价格\t\t\t数量");
                    for (int i=0;i<menusorder.length;i++){
                        if(menusorder[i]!=null){
                            System.out.println(menusorder[i].id+"\t\t\t\t"+menusorder[i].mName+"\t\t\t"+menusorder[i].mPrice+"\t\t\t"+menusorder[i].count);
                        }
                    }
                    System.out.println("");
                    break;
                case 3:
                    //todo 付款
                    System.out.println("编号\t\t\t\t名称\t\t\t价格\t\t\t数量");
                    for (int i=0;i<menusorder.length;i++){
                        if(menusorder[i]!=null){
                            System.out.println(menusorder[i].id+"\t\t\t\t"+menusorder[i].mName+"\t\t\t"+menusorder[i].mPrice+"\t\t\t"+menusorder[i].count);
                        }
                    }
                    System.out.println("请选择您需要付款的类型：1：全部付款,2：部分付款");
                    int i = scanner.nextInt();
                    if(i==1){
                        Order order = new Order();
                        //todo 全部付款
                        System.out.println("编号\t\t\t\t名称\t\t\t价格\t\t\t数量");
                        for (int i3=0;i3<menusorder.length;i3++){
                            if(menusorder[i3]!=null){
                                System.out.println(menusorder[i3].id+"\t\t\t\t"+menusorder[i3].mName+"\t\t\t"+menusorder[i3].mPrice+"\t\t\t"+menusorder[i3].count);
                            }
                        }
                        double count = SumPrice(menusorder);
                        System.out.println("以上是付款的菜品信息");
                        System.out.println("总价格是："+count);
                        System.out.println("请选择支付方式：1：线上支付，2：会员卡支付");
                        int i2 = scanner.nextInt();
                        if(i2==1){
                            System.out.println("请输入支付金额：");
                            double v = scanner.nextDouble();
                            order.orderId=0001;
                            order.menus=menusorder;
                            order.status="已支付";
                            order.countPrice=count;
                        }else if(i2==2){
                            //2：会员卡支付

                        }
                        //循环存入订单容器
                        orders[0]=order;
                        System.out.println("支付成功");
                    }else if(i==2){

                    }

                    break;

                case 4:
                    //todo 查看订单


                    break;
            }
        }while (isGo);


    }

    //验证是否是 老菜
    public static boolean checkMenuName(Menu[]  menusorder,int id){
        for (int i = 0; i < menusorder.length; i++) {
            if (menusorder[i] != null) {
                if(menusorder[i].id==id){
                    menusorder[i].count++;
                    return false;
                }
            }
        }
        return true;
    }

    //查找菜品
    public static Menu checkFromMenuList(Menu[]  menus,int id){
        for (int i = 0; i < menus.length; i++) {
            if (menus[i] != null) {
                if(menus[i].id==id){
                    return menus[i];
                }
            }
        }
        return null;
    }

    //重名验证
    public static boolean checkName(User[] users,String name) {
        for (int i = 0; i < users.length; i++) {
            if (users[i] != null) {
                if(users[i].name.equals(name)){
                    return false;
                }
            }
        }
        return true;
    }

    //登录
    public static boolean login(User[] users,String uname,String password){
        for (int i=0;i<users.length;i++){
            if(users[i] !=null ){
                if(uname.equals(users[i].name) && password.equals(users[i].password)){

                    return true;
                }
            }
        }
        return false;
    }
    //计算总价格
    public static double  SumPrice(Menu[]  menusorder){
        double count=0;
        for (int i3=0;i3<menusorder.length;i3++){
            if(menusorder[i3]!=null){
                count+= (menusorder[i3].mPrice*menusorder[i3].count);
            }
        }
        return count;
    }
}
